package server.service;

import com.qqcommon.Message;
import com.qqcommon.MessageType;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author wty
 * @date 2022/10/25 15:34
 */
@SuppressWarnings({"all"})
public class ServerConnectionThread extends Thread {
    private Socket socket = null;
    // private Object object = new Object();
    private String userID;

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

  /*  public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }*/

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    //public ServerConnectionThread(String userID, Socket socket, Object object) {
    public ServerConnectionThread(String userID, Socket socket) {
        this.socket = socket;
        //this.object = object;
        this.userID = userID;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("服务端和客户端: " + userID + " 保持通信，等待客户端发送…………");

            ObjectOutputStream objectOutputStream = null;
            ObjectInputStream objectInputStream = null;
            try {
                objectInputStream = new ObjectInputStream(socket.getInputStream());
                Object o = objectInputStream.readObject();
                // 向下转型
                Message message = (Message) o;
                // 后面使用这个message
                if (message.getType().equals(MessageType.MESSAGE_ONLINEFRIEND)) {
                    System.out.println(message.getSender() + "需要查询用户在线列表");
                    String onlineUser = ManageServerConnectServiceThread.getOnlineUser();
                    Message message1 = new Message();
                    message1.setType(MessageType.MESSAGE_ONLINEFRIEND);
                    message1.setContent(onlineUser);
                    message1.setReceiver(message.getSender());
                    // 写入到数据通道
                    objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(message1);

                } else if (message.getType().equals(MessageType.MESSAGE_COMMON)) {
                    // 普通消息
                    // 通过集合获取socket 这里是获取接收方的线程，相当于打通服务器和接收客户端的socket
                    // 私发
                    ServerConnectionThread serverConnectionThread = ManageServerConnectServiceThread.get(message.getReceiver());
                    // 判断用户是否离线
                    if (null == serverConnectionThread || "".equals(serverConnectionThread)) {
                        // 离线用户
                        OffLineMessageThread.put(message.getReceiver(), message);
                    } else {
                        // 在线用户
                        Socket socket = serverConnectionThread.getSocket();
                        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(socket.getOutputStream());

                        // 这里服务端类似一个中转站，把消息再转发给对应的客户端即可
                        objectOutputStream1.writeObject(message); // 目前只能发在线的(离线可以尝试保存数据库)
                    }


                } else if (message.getType().equals(MessageType.MESSAGE_EVERYONE)) {
                    // 群发消息
                    // 通过集合获取socket 这里是获取接收方的线程，相当于打通服务器和接收客户端的socket

                    String onlineUsers = ManageServerConnectServiceThread.getOnlineUserExceptUser(message.getSender()); // 剔除掉自己的成员列表
                    String[] users = onlineUsers.split(" ");
                    for (int i = 0; i < users.length; i++) {
                        Socket socket = ManageServerConnectServiceThread.get(users[i]).getSocket();
                        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(socket.getOutputStream());
                        objectOutputStream1.writeObject(message);
                    }


                } else if (message.getType().equals(MessageType.MESSAGE_FILE)) {
                    String receiver = message.getReceiver();
                    ServerConnectionThread sct = ManageServerConnectServiceThread.get(receiver);
                    if (null == sct || "".equals(sct)) {
                        // 离线文件，先往集合中增加一个元素
                        OffLineMessageThread.put(message.getReceiver(), message);
                        System.out.println(message.getSender() + "发送的用户：" + receiver + "未上线，" + message.getPath() + "文件将离线发送!");

                        // 离线的话，文件暂时不发送,给发送方发送一条普通消息，提示对方未上线
                        Message message1 = new Message();
                        message1.setSender(message.getSender());
                        Socket socketFile = ManageServerConnectServiceThread.get(message1.getSender()).getSocket();
                        message1.setType(MessageType.MESSAGE_COMMON);
                        message1.setReceiver(message.getSender());
                        message1.setSender("系统提示");
                        message1.setSendTime(message.getSendTime());
                        message1.setContent("您发送的用户：" + receiver + "未上线，将离线发送!");
                        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(socketFile.getOutputStream());
                        objectOutputStream1.writeObject(message1);

                    } else {
                        // 在线就发送文件
                        // 发送文件
                        Socket socket = sct.getSocket();
                        // 对象流转发给指定的用户
                        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream(socket.getOutputStream());
                        objectOutputStream1.writeObject(message);
                    }

                } else if (message.getType().equals(MessageType.MESSAGE_EXIT)) {
                    // 退出系统
                    System.out.println(message.getSender() + "退出系统");
                    // 把该用户的socket从集合中移除
                    ManageServerConnectServiceThread.remove(message.getSender());
                    socket.close(); // 关闭连接
                    break; // 退出循环
                } else {
                    System.out.println("其它类型的message,暂时不处理");
                }


                //objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                //objectOutputStream.writeObject(object);
                //objectOutputStream.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } // while循环
    }
}
